package collection.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Random;
import java.util.Vector;

class IntComparator<T> implements Comparator<T>{

	@Override
	public int compare(T o1, T o2) {
		int n1 = (Integer) o1;
		int n2 = (Integer) o2;
		
		if (n1 > n2) return 1;
		else if (n1 < n2) return -1;
		else return 0;
	}
	
}

/* Sorting random generate integer elements 
 * Compare ArrayList and Vector performance
 * */
public class SortListComparison {
	final static int NOFE = 5000000;
	final static int NRANGE = 10000;
	
	ArrayList<Integer> alist;
	Vector<Integer> vec;
	LinkedList<Integer> llist;
	
	public SortListComparison() {
		alist = new ArrayList<>();
		vec = new Vector<>();
		llist = new LinkedList<>();
		randomGenerate();
	}

	private void randomGenerate() {
		Random ran = new Random();
		for (int i=0; i<NOFE; i++){
			int num = ran.nextInt(NRANGE);
			alist.add(new Integer(num));
			vec.add(new Integer(num));
			llist.add(new Integer(num));
		}
	}
	
	public void sortArrayList(){
		Collections.sort(alist, new IntComparator<Integer>());
//		for (Integer i : alist)
//			System.out.print(i + " ");
//		System.out.println();
	}
	
	public void sortVector(){
		Collections.sort(vec, new IntComparator<Integer>());
//		for (Integer i : vec)
//			System.out.print(i + " ");
//		System.out.println();
	}

	public void sortLList(){
		Collections.sort(llist, new IntComparator<Integer>());
//		for (Integer i : llist)
//			System.out.print(i + " ");
//		System.out.println();
	}

	public static void main(String[] args) {
		long sortTimeAList, sortTimeVector, sortTimeLList;
		SortListComparison sorter = new SortListComparison();

		sortTimeLList = System.currentTimeMillis();
		sorter.sortLList();
		sortTimeLList = System.currentTimeMillis() - sortTimeLList;
		
		sortTimeVector = System.currentTimeMillis();
		sorter.sortVector();
		sortTimeVector = System.currentTimeMillis() - sortTimeVector;

		sortTimeAList = System.currentTimeMillis();
		sorter.sortArrayList();
		sortTimeAList = System.currentTimeMillis() - sortTimeAList;
		
		System.out.println("Time for sorting a array list: " + sortTimeAList);
		System.out.println("Time for sorting a vector: " + sortTimeVector);
		System.out.println("Time for sorting a linked list: " + sortTimeVector);
	}
}
